{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 简单线性回归教程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### —— 用最小二乘法求解一元线性回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy import *\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 导入数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们用一组现成的数据，文件 data.csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAGrlJREFUeJzt3XuMXVd1x/HvyniASdoyhIyiZIyxEZEtwCSGEQS5QsQBHCBKLINoUmjDo4qqopZCa7BV1EIFtZFbAf2jVCmvVKEhaRIcHi0mio2QojpojAMmEJfwiONJggclpiIZyNhZ/eOemxnP3Me597z22ef3kayZe+bOnX3PeNbZe+219zF3R0RE4nVG1Q0QEZFiKdCLiEROgV5EJHIK9CIikVOgFxGJnAK9iEjkFOhFRCKnQC8iEjkFehGRyK2ougEA55xzjq9evbrqZoiI1MrBgwd/6e4T/Z4XRKBfvXo109PTVTdDRKRWzOyBNM9T6kZEJHJ9A72Zfc7MjpvZDxYd221m95nZ983sy2Y2vuhrO8zsfjM7Ymabi2q4iIikk6ZH/wXgsiXH7gBe4u4vBf4X2AFgZi8CrgJenHzPv5jZSG6tFRGRgfUN9O7+beDRJce+6e4nk4cHgJXJ51cCX3L337r7z4D7gVfk2F4RERlQ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Ivf/B1QcPgurGQAAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "points = genfromtxt('data.csv', delimiter=',')\n",
    "N = len(points)\n",
    "\n",
    "# 提取 x，y\n",
    "x = array(points[:,0])\n",
    "y = array(points[:,1])\n",
    "\n",
    "# 用plt做出散点图\n",
    "plt.scatter(x,y)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 定义损失函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义线性模型 y = mx + b\n",
    "\n",
    "def compute_cost(points, b, m):\n",
    "    total_cost = 0\n",
    "    \n",
    "    N = len(points)\n",
    "    \n",
    "    # 计算所有误差平方的和 sum(y-mx-b)^2\n",
    "    for i in range(N):\n",
    "        x = points[i, 0]\n",
    "        y = points[i, 1]\n",
    "        \n",
    "        total_cost += (y - m * x - b) ** 2\n",
    "    \n",
    "    return total_cost/float(N)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3. 定义拟合算法函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 需要计算 x_avg\n",
    "def average(data):\n",
    "    sum = 0\n",
    "    num = len(data)\n",
    "    for i in range(num):\n",
    "        sum += data[i]\n",
    "    return sum/float(num)\n",
    "\n",
    "def fit(points):\n",
    "    N = len(points)\n",
    "    x_bar = average(array(points[:, 0]))\n",
    "    \n",
    "    sum_yx = 0\n",
    "    sum_x2 = 0\n",
    "    \n",
    "    sum_b = 0\n",
    "    \n",
    "    # 求m\n",
    "    for i in range(N):\n",
    "        x = points[i, 0]\n",
    "        y = points[i, 1]\n",
    "        sum_yx += y * (x - x_bar)\n",
    "        sum_x2 += x ** 2\n",
    "        \n",
    "    m = sum_yx / ( sum_x2 - x_bar **2 * float(N))\n",
    "    \n",
    "    # 求b\n",
    "    for i in range(N):\n",
    "        x = points[i, 0]\n",
    "        y = points[i, 1]\n",
    "        sum_b += y - m * x\n",
    "        \n",
    "    b = sum_b/ float(N)\n",
    "    \n",
    "    return m, b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4. 测试：运行fit函数来计算最优的m和b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "m is: 1.3224310227553846\n",
      "b is: 7.991020982269173\n",
      "cost is: 110.25738346621313\n"
     ]
    }
   ],
   "source": [
    "m, b = fit(points)\n",
    "\n",
    "print(\"m is:\",m)\n",
    "print(\"b is:\",b)\n",
    "\n",
    "# 计算损失函数\n",
    "cost = compute_cost(points, b, m)\n",
    "\n",
    "print(\"cost is:\", cost)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5. 画出拟合曲线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzt3X94XVWd7/H3t2mAgEKs7YM0pSaOnTJIgdYIYu8oFLQgFTKVQZAZGWTs3BG8iE5ty0VnnAu2naqAg3JFUFAQWgEDtEBl2urcp1qwbYBSIVjtDxp+tAhBsBHSdN0/9klzcrLP7332r/N5PU+f5OyzzzkrO813r/1d37W2OecQEZH0GhV1A0REpLYU6EVEUk6BXkQk5RToRURSToFeRCTlFOhFRFJOgV5EJOUU6EVEUk6B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vgo0Fcgk6p4DNgFPAz8Duh1zu3N7LITqJdF5q8FvgTsyzx+O/V7LBzwMzPbYGZzMtsOd849n/n+BeDwaJoWujZgN/CDTFrvJjM7hPo9HoPOA+7IfB/asVCgr4BzbiBzGTYBOAE4KuImRcLMZgG7nHMbom5LTPwP59w04AzgEjP7YPaTzitxq5cyt9HANOAG59xU4E/kpCbq7HiQGas6C/hJ7nO1PhYK9FXIXIquAU4Cms1s8I5dE4CeyBoWnunAWWa2DbgTL2VzHfV5LHDO9WS+7sLLwZ4AvGhmRwBkvu6KroWh2gnsdM49knl8F17gr9fjAV4HYKNz7sXM49COhQJ9mcxsnJk1Z75vAj6MN8i0Bjgns9uFwL3RtDA8zrkFzrkJzrlWvEvS1c65C6jDY2Fmh5jZWwe/Bz4CPAnch3cMoE6OBYBz7gXgWTMbvNP9qcBvqNPjkXE+Q2kbCPFYaMJUmczsWLyBkwa8E+Uy59y/m9m78Hq1Y4Au4O+cc29E19JwmdnJwL8452bV47HI/Mw/zTwcDfzYOXe1mb0dWAZMxFuh9Vzn3MsRNTNUZnY83iD9AcDvgYvI/M1QZ8cjc/LfAbzLOfdqZlto/zcU6EVEUk6pGxGRlFOgFxFJOQV6EZGUU6AXEUk5BXoRkZRToBcRSTkFehGRlFOgFxFJuf8PVZxc4BXYvoIAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x, y)\n",
    "\n",
    "# 针对每一个x，求出预测值\n",
    "y_pred = m * x + b\n",
    "\n",
    "plt.plot(x, y_pred, c='r')\n",
    "\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
